﻿#include <iostream>
#include "defaults.h"
#include "uWS.h"
#include <iostream>
#include <chrono>
#include <cmath>
#include <thread>
#include <fstream>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <atomic>
#include "peer_connection_wsclient.h"
#include "rtc_base/json.h"
#include "JanusHandle.h"
#include "JanusTransaction.h"
#include <list>
#include "video_room_client.h"

#include "defaults.h"
#include "peer_connection.h"


#if defined(WEBRTC_WIN)
#include "rtc_base/win32.h"
#endif  // WEBRTC_WIN

void ConfigWebrtcFileLog() {
	class FileLog : public rtc::LogSink {
	public:
		FileLog(const std::string& LogPath)
			:logfile_(NULL),
			log_path_(LogPath) {}

		virtual ~FileLog() {
			if (logfile_) {
				fclose(logfile_);
				logfile_ = NULL;
			}
		}
		inline void Start(void)
		{
#define MAX_LOG_FILE_SIZE (1024*1024)

			if (NULL == logfile_) {
				logfile_ = fopen(logfileName_.c_str(), "w");
			}
		}

		inline void Close(void)
		{
			if (logfile_) {
				fclose(logfile_);
				logfile_ = NULL;
			}
		}

		virtual void OnLogMessage(const std::string& message) {
			rtc::CritScope lock(&log_crit_);
			Start();

			if (NULL == logfile_)
				return;

			string msgString;
			msgString = message;
			if (fwrite(msgString.c_str(), 1, msgString.length(), logfile_) < 0) {
				Close();
			}
			else if (fflush(logfile_) < 0) {
				Close();
			}
		}
	private:

		FILE* logfile_;
		const std::string log_path_;
		std::string logfileName_ = "webrtclog.txt";
		rtc::CriticalSection log_crit_;
	};

	rtc::LoggingSeverity min_sev = rtc::LS_WARNING;
	min_sev = rtc::LS_INFO;
	FileLog* _LogStream = new FileLog("logs");
	rtc::LogMessage::AddLogToStream(_LogStream, min_sev);
}

int main()
{
	ConfigWebrtcFileLog();
    rtc::EnsureWinsockInit();
    rtc::Win32SocketServer w32_ss;
    rtc::Win32Thread w32_thread(&w32_ss);
    rtc::ThreadManager::Instance()->SetCurrentThread(&w32_thread);

    auto network_thread_ = rtc::Thread::CreateWithSocketServer();
    network_thread_->SetName("network_thread", nullptr);
    network_thread_->Start();

    auto worker_thread_ = rtc::Thread::Create();
    worker_thread_->SetName("worker_thread", nullptr);
    worker_thread_->Start();

    std::unique_ptr<Thread> signaling_thread_ = rtc::Thread::Create();
    signaling_thread_->SetName("signaling_thread", nullptr);
    signaling_thread_->Start();

    VideoRoomClient*client = new VideoRoomClient();

    client->ConectToServer("janusdemo.com", 8188);
	while (true) {
		Sleep(500);
		for (auto x : client->m_peer_connection_map) {
			RTC_LOG(INFO) << "list client: "
				<< x.first ;
			RTC_LOG(INFO) << "transceivers: " <<
				x.second->peer_connection_->GetTransceivers().size() 
				<< "receivers: "
				<< x.second->peer_connection_->GetTransceivers().size();
			RTC_LOG(INFO) << "signal state: " << x.second->peer_connection_->signaling_state();
			RTC_LOG(INFO) << "ice_connection_state: " << x.second->peer_connection_->ice_connection_state();
			RTC_LOG(INFO) << "ice_gathering_state: " << x.second->peer_connection_->ice_gathering_state();
		}
    }

}

